from Xlib import Xatom


class EWMHManager:
    """
    The EWMH Manager adds a layer between EWMH messaging on the X server side
    and properties that the client can set If you don't know what EWMH means,
    don't worry just see the EWMH specification on the web.Briefly EWMH
    specifies certain standards for smooth operation of different desktop compo-
    nents.For e.g you can specify the number of desktops using
    _NET_NUMBER_OF_DESKTOPS property and any other application like gnome-panel
    or kicker can access it and do whatever it wants to do with it,like showing
    a desktop switcher with the correct number of desktops
    For this we usee EWMH atoms.Atoms can be said to be language or platform
    independent data structures or message types supported by X.
    These so called EWMH atoms map to properties on the root window that
    you can set.
    """
    def __init__(self, disp):
        """
        This method takes the currently active display and gets a handle for the
        root window and sets the various atoms describing corresponding
        EWMH properties by calling setAtoms 
        """
        self.disp = disp
        defaultScreen = self.disp.screen(
                                         self.disp.get_default_screen()
                                         )
        self.root = defaultScreen.root
        self.setAtoms(self.disp)
    
    def setAtoms(self, dsp):
        """ GET necessary atoms for window properties """   
        self._ABOVE = dsp.intern_atom("_NET_WM_STATE_ABOVE")
        #Checks whether a window is above other windows
        self._BELOW = dsp.intern_atom("_NET_WM_STATE_BELOW")
        #Checks whether a window is below othher windows
        self._CLIENT_LIST_STACKING = dsp.intern_atom("_NET_CLIENT_LIST_STACKING")
        #An atom specifying a list of Window ids sorted by the stacking order.
        #You can also do some magic stuff here.For example when using virtual desktop
        #you can show only the number of windows on the current desktop
        self._CLIENT_LIST = dsp.intern_atom("_NET_CLIENT_LIST")
        #An atom specifying a list of Window ids showing existing windows
        self._CURRENT_DESKTOP = dsp.intern_atom("_NET_CURRENT_DESKTOP")
        self._DESKTOP = dsp.intern_atom("_NET_WM_DESKTOP")
        self._DESKTOP_COUNT = dsp.intern_atom("_NET_NUMBER_OF_DESKTOPS")
        self._DESKTOP_NAMES = dsp.intern_atom("_NET_DESKTOP_NAMES")   
        self._HIDDEN = dsp.intern_atom("_NET_WM_STATE_HIDDEN")
        self._ICON = dsp.intern_atom("_NET_WM_ICON")
        self._NAME = dsp.intern_atom("_NET_WM_NAME")
        self._RPM = dsp.intern_atom("_XROOTPMAP_ID")
        self._SHADED = dsp.intern_atom("_NET_WM_STATE_SHADED")
        self._SHOWING_DESKTOP = dsp.intern_atom("_NET_SHOWING_DESKTOP")
        self._SKIP_PAGER = dsp.intern_atom("_NET_WM_STATE_SKIP_PAGER")
        self._SKIP_TASKBAR = dsp.intern_atom("_NET_WM_STATE_SKIP_TASKBAR")
        self._STATE = dsp.intern_atom("_NET_WM_STATE")
        self._STICKY = dsp.intern_atom("_NET_WM_STATE_STICKY")
        self._STRUT = dsp.intern_atom("_NET_WM_STRUT")
        self._STRUTP = dsp.intern_atom("_NET_WM_STRUT_PARTIAL")
        self._WMSTATE = dsp.intern_atom("WM_STATE")   
        self._WORKAREA = dsp.intern_atom("_NET_WORKAREA")
        self._ACTIVE_WINDOW = dsp.intern_atom("_NET_ACTIVE_WINDOW")
    
    def setEWMHworkarea(self, x, y, width, height):
        self.root.change_property(self._WORKAREA, Xatom.CARDINAL, 32, [x, y, width, height])
    
    def setEWMHcurrentdesktop(self, desktop):
        """
        Set the current desktop on _NET_CURRENT_DESKTOP property on root window
        """
        self.root.change_property(self._CURRENT_DESKTOP, Xatom.CARDINAL, 32, [desktop])
    
    def setEWMHdesktopcount(self, count):
        """
        Set the number desktop on _NET_NUMBER_OF_DESKTOPS property on root window
        """
        self.root.change_property(self._DESKTOP_COUNT, Xatom.CARDINAL, 32, [count])
    
    def setEWMHclientlist(self, winlist):
        # print "EWMH_CLIENT LIST"
        # print winlist
        self.root.change_property(self._CLIENT_LIST, Xatom.WINDOW, 32, winlist)
                       
    def setEWMHstackedclientlist(self, winlist):
        #print "EWMH_STACKED CLIENT LIST"
        #print winlist
        self.root.change_property(self._CLIENT_LIST_STACKING, Xatom.WINDOW, 32, winlist)
                     
    def setEWMHactivewindow(self, win):
        self.root.change_property(self._ACTIVE_WINDOW, Xatom.CARDINAL, 32, win)

    def setEWMHwindowdesktop(self, win, desktop):
        win.change_property(self._DESKTOP, Xatom.CARDINAL, 32, [desktop])
        
    def setEWMHwindowstate(self, win, winstateatomlist):
        win.change_property(self._STATE, Xatom.ATOM, 32, winstateatomlist)
        
    def setEWMHWMState(self, win, windowstate):
        win.change_property(self._WMSTATE, Xatom.ATOM, 32, windowstate)
        

